{% import "components/user.html" as user with context %}
{% macro reply(mode_create, udoc, doc = {}, rdoc = {}) %}

{% endmacro %}

{% macro render(
  view, docs, udict,
  comment_ref,
  reply_ref,
  comment_post_perm,
  comment_edit_perm,
  comment_edit_self_perm,
  comment_delete_perm,
  comment_delete_self_perm,
  reply_post_perm,
  reply_edit_perm,
  reply_edit_self_perm,
  reply_delete_perm,
  reply_delete_self_perm,
  comment_placeholder = 'Write Your Comment',
  comment_post_op = 'reply',
  comment_edit_op = 'edit_reply',
  comment_post_text = 'Comment',
  comment_edit_text = 'Update',
  reply_post_op = 'tail_reply',
  reply_post_text = 'Reply',
  reply_edit_op = 'edit_tail_reply',
  reply_edit_text = 'Update',
  comment_delete_op = 'delete_reply',
  reply_delete_op = 'delete_tail_reply'
) %}
<ul class="dczcomments__list view--solution">
{% if handler.user.hasPerm(comment_post_perm) %}
<li class="dczcomments__item">
  <div class="media">
    <div class="media__left top">
      <div class="vote vote--discussion">
        <div class="vote-number">0</div>
      </div>
    </div>
    <div class="media__body top">
      <div class="clearfix">
        <div class="supplementary dczcomments__supplementary">
          {{ user.render_inline(handler.user) }}
        </div>
      </div>
      <div class="hasjs--hide">
        <form method="post" class="dczcomments__box" name="dczcomments__box__form" data-hotkey="ctrl+enter:submit,esc:vjCommentBoxCancel">
          <div>
            <textarea name="content" class="textbox" data-markdown required></textarea>
          </div>
          <div>
            <input type="submit" class="rounded primary button dczcomments__box__submit" value="{{ _(comment_post_text) }} (Ctrl+Enter)" data-value-reply="{{ _(reply_post_text) }} (Ctrl+Enter)" data-value-comment="{{ _(comment_post_text) }} (Ctrl+Enter)" data-value-comment-update="{{ _(comment_edit_text) }} (Ctrl+Enter)" data-value-reply-update="{{ _(reply_edit_text) }} (Ctrl+Enter)">
            <input type="button" class="rounded button nojs--hide" name="dczcomments__box__cancel" value="{{ _('Cancel') }} (Esc)">
          </div>
          <input type="hidden" name="operation" value="{{ comment_post_op }}">
        </form>
      </div>
      <div class="nojs--hide">
        <textarea class="textbox" name="dczcomments__dummy-box" readonly data-form="{{ {operation: comment_post_op}|json }}" placeholder="{{ _(comment_placeholder) }}"></textarea>
      </div>
      <div class="commentbox-placeholder"></div>
      <ul style="display:none">
        <li class="dczcomments__reply commentbox-container">
          <div class="media">
            <div class="media__body top">
              <div class="clearfix">
                <div class="supplementary dczcomments__supplementary">
                  {{ user.render_inline(handler.user, badge=false) }}
                </div>
              </div>
              <div class="commentbox-placeholder"></div>
            </div>
          </div>
        </li>
      </ul>
    </div>
  </div>
</li>
{% endif %}
{%- for doc in docs -%}
  {% set udoc = udict[doc.owner] %}
  <li class="dczcomments__item">
  <div class="media">
    <div class="media__left top">
      <div class="vote vote--discussion">
      {% if handler.user.hasPerm(perm.PERM_VOTE_PROBLEM_SOLUTION) %}
        <div class="vote-number rotator--enabled">{{ doc.vote }}</div>
        <form method="post" class="vote-op clearfix">
          <input type="hidden" name="psid" value="{{ doc._id }}">
          <button type="submit" name="operation" value="upvote" class="vote-button upvote{% if pssdict[doc._id].vote == 1 %} active{% endif %}" data-tooltip="{{ _('Upvote') }}" data-tooltip-pos="bottom center" aria-label="{{ _('Upvote') }}"><span class="icon icon-vote--up"></span></button>
          <button type="submit" name="operation" value="downvote" class="vote-button downvote{% if pssdict[doc._id].vote == -1 %} active{% endif %}" data-tooltip="{{ _('Downvote') }}" data-tooltip-pos="bottom center" aria-label="{{ _('Downvote') }}"><span class="icon icon-vote--down"></span></button>
        </form>
      {% else %}
        <div class="vote-number">{{ doc.vote }}</div>
      {% endif %}
      </div>
    </div>
    <div class="media__body top">
      <div class="clearfix">
        <div class="supplementary dczcomments__supplementary">
          {{ user.render_inline(udoc) }} @ {{ datetimeSpan(doc._id)|safe }}
        </div>
        <div class="dczcomments__operations nojs--hide">
        {% if pdoc %}
          <a href="javascript:;" data-tooltip="{{ _('Copy Link') }}" data-copylink="{{ url('problem_solution_detail', pid=pdoc.pid|default(pdoc.docId), sid=doc._id) }}">
            <span class="icon icon-link"></span>
          </a>
        {% endif %}
        {% if handler.user.hasPerm(reply_post_perm) %}
          <a href="javascript:;" data-tooltip="{{ _('Reply') }}" data-op="reply" data-type="comment" 
            data-form="{{ {operation: reply_post_op, psid: doc._id}|json }}"
          ><span class="icon icon-reply"></span></a>
        {% endif %}
        {% if (handler.user.hasPerm(comment_edit_self_perm) and handler.user.own(doc)) or handler.user.hasPerm(comment_edit_perm) %}
          <a href="javascript:;" data-tooltip="{{ _('Edit') }}" data-op="edit" data-type="comment"
            data-form="{{ {operation: comment_edit_op, psid: doc._id}|json }}"
          ><span class="icon icon-edit"></span></a>
        {% endif %}
        {% if (handler.user.hasPerm(comment_delete_self_perm) and handler.user.own(doc)) or handler.user.hasPerm(comment_delete_perm) %}
          <a href="javascript:;" data-tooltip="{{ _('Delete') }}" data-op="delete" data-type="comment"
            data-form="{{ {operation: comment_delete_op, psid: doc._id}|json }}"
          ><span class="icon icon-delete"></span></a>
        {% endif %}
        </div>
      </div>
      <div class="typo richmedia" data-emoji-enabled data-raw-url="{{ url('problem_solution_raw', pid=doc.parentId, psid=doc._id) }}">
        {{ doc['content']|markdown|safe }}
      </div>
      <div class="commentbox-edit-target"></div>
      <ul class="dczcomments__replies commentbox-reply-target">
      {%- for rdoc in doc['reply'] -%}
        {% set udoc = udict[rdoc['owner']] %}
        <li class="dczcomments__reply{% if mode_create %} commentbox-container{% endif %}">
          <div class="media">
            <div class="media__body top">
              <div class="clearfix">
                <div class="supplementary dczcomments__supplementary">
                  {{ user.render_inline(udoc, badge=false) }} @ {{ datetimeSpan(rdoc['_id'])|safe }}
                </div>
                <div class="dczcomments__operations nojs--hide">
                {% if handler.user.hasPerm(reply_post_perm) %}
                  <a href="javascript:;" data-tooltip="{{ _('Reply') }}" data-op="reply" data-type="reply"><span class="icon icon-reply"></span></a>
                {% endif %}
                {% if (handler.user.hasPerm(reply_edit_self_perm) and handler.user.own(rdoc)) or handler.user.hasPerm(reply_edit_perm) %}
                  <a href="javascript:;" data-tooltip="{{ _('Edit') }}" data-op="edit" data-type="reply"
                    data-form="{{ {operation: reply_edit_op, psid: doc._id, psrid: rdoc._id}|json }}"
                  ><span class="icon icon-edit"></span></a>
                {% endif %}
                {% if (handler.user.hasPerm(reply_delete_self_perm) and handler.user.own(rdoc)) or handler.user.hasPerm(reply_delete_perm) %}
                  <a href="javascript:;" data-tooltip="{{ _('Delete') }}" data-op="delete" data-type="reply"
                    data-form="{{ {operation: reply_delete_op, psid: doc._id, psrid: rdoc._id}|json }}"
                  ><span class="icon icon-delete"></span></a>
                {% endif %}
                </div>
              </div>
              <div class="typo richmedia" data-emoji-enabled data-raw-url="{{ url('problem_solution_reply_raw', pid=doc.parentId, psid=doc._id, psrid=rdoc._id) }}">
                {{ rdoc['content']|markdown|safe }}
              </div>
              <div class="commentbox-edit-target"></div>
              </div>
          </div>
        </li>
      {%- endfor -%}
      </ul>
    </div>
  </div>
</li>
{%- endfor -%}
</ul>
{% endmacro %}
